package com.file.system.service.impl.system;

import cn.hutool.core.util.IdUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.sort.BaseSort;
import com.file.system.entity.system.WebDepartment;
import com.file.system.mapper.system.WebDepartmentMapper;
import com.file.system.service.system.IWebDepartmentService;
import com.file.system.tools.page.DP;
import com.file.system.tools.sort.SortUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static cn.hutool.core.util.ObjectUtil.isEmpty;
import static cn.hutool.core.util.ObjectUtil.isNull;

/**
 * 部门 实现
 *
 * @author jch
 * @since 2022-08-04
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WebDepartmentServiceImpl implements IWebDepartmentService {

    @Autowired
    private WebDepartmentMapper mapper;
    @Autowired
    private SortUtil sortUtil;

    //添加部门
    public boolean save(WebDepartment webDepartment) {
        webDepartment.setId(IdUtil.getSnowflakeNextId());
        //排序
        webDepartment = sortUtil.add(webDepartment, webDepartment.getClass(), "web_department",
                BaseSort.eq().val("is_del", 0),
                BaseSort.eq().val("pid", webDepartment.getPid())
        );
        if (isNull(webDepartment)) {
            return false;
        }
        return SqlHelper.retBool(mapper.save(webDepartment));
    }


    //删除部门
    public boolean removeById(@Param("id") Long id) {
        //排序
        WebDepartment webDepartment = mapper.selectById(id);
        sortUtil.del(webDepartment, webDepartment.getClass(), "web_department",
                BaseSort.eq().val("is_del", 0),
                BaseSort.eq().val("pid", webDepartment.getPid())
        );
        return SqlHelper.retBool(mapper.removeById(new WebDepartment().setId(id)));
    }

    //编辑部门
    public boolean updateById(@Param("et") WebDepartment webDepartment) {
        if (isEmpty(webDepartment.getId())) {
            return false;
        }
        //查询
        WebDepartment webDepartmentOld = mapper.selectById(webDepartment.getId());
        //排序
        webDepartment = sortUtil.update(webDepartment, webDepartment.getClass(), webDepartmentOld.getSort(), "web_department",
                BaseSort.eq().val("is_del", 0),
                BaseSort.eq().val("pid", webDepartment.getPid())
        );
        if (isNull(webDepartment)) {
            return false;
        }
        return SqlHelper.retBool(mapper.updateById(webDepartment));
    }

    //编辑
    public int update(@Param("ew") UpdateWrapper<WebDepartment> query) {
        return mapper.update(query);
    }

    //添加部门(批量)
    public int saveBatch(@Param("list") List<WebDepartment> webDepartmentList) {
        if (webDepartmentList.size() < 1) return 0;
        return mapper.saveBatch(webDepartmentList);
    }

    //查询部门
    @DS("slave")
    public List<WebDepartment> list(@Param("ew") QueryWrapper<WebDepartment> query) {
        return mapper.select(query);
    }

    //通过ids查询部门集合
    @DS("slave")
    public List<WebDepartment> listByIds(List<Long> ids) {
        QueryWrapper<WebDepartment> query = new QueryWrapper<>();
        query.in("a.id", ids);
        query.orderByAsc("a.sort");
        return mapper.select(query);
    }

    //查询所有非删除状态部门
    @DS("slave")
    public List<WebDepartment> listAll() {
        QueryWrapper<WebDepartment> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
        query.orderByAsc("a.sort");
        return mapper.select(query);
    }

    //查询部门分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("ew") QueryWrapper<WebDepartment> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<WebDepartment> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<WebDepartment> list = mapper.select(page, query);
        return DataPage.init().set(count, list);
    }

    //通过id查询部门
    @DS("slave")
    public WebDepartment getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询部门
    @DS("slave")
    public WebDepartment getOne(@Param("ew") QueryWrapper<WebDepartment> query) {
        List<WebDepartment> list = mapper.select(query);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询部门数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<WebDepartment> query) {
        return mapper.selectCount(query);
    }
}
